package com.example.demo.util;


import java.security.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;


public class DateUtils {

    private static final SimpleDateFormat defaultFormat=new SimpleDateFormat("yyyy-MM-dd");
    public static final String DEFAULT_FORMAT="yyyy-MM-dd";
    public static final String FORMATE1="yyyy";
    public static final String FORMATE2="yyyy-MM";
    public static final String FORMATE3="yyyy-MM-dd";
    public static final String FORMATE4="yyyy-MM-dd HH";
    public static final String FORMATE5="yyyy-MM-dd HH:mm";
    public static final String FORMATE6="yyyy-MM-dd HH:mm:ss";
    public static final String FORMATE7="yyyy年";
    public static final String FORMATE8="yyyy年MM月";
    public static final String FORMATE9="yyyy年MM月dd日";
    public static final String FORMATE10="yyyy年MM月dd日 HH时";
    public static final String FORMATE11="yyyy年MM月dd日 HH时mm分";
    public static final String FORMATE12="yyyy年MM月dd日 HH时mm分ss秒";
    public static final String FORMATE13="dd-yyyy";
    public static final String FORMATE14="dd-MM-yyyy";
    public static final String FORMATE15="dd日MM月yyyy年";
    public static final String FORMATE16="yyyyMM";
    public static final String FORMATE17="yyyyMMdd";
    public static final String FORMATE18="dd/MM/yyyy";
    public static final String FORMATE19="dd/MM/yyyy HH:mm:ss";
    public static final String FORMATE20="dd/MM/yyyy HH:mm";
    public static final String FORMATE21="dd/MM/yyyy HH";

    public static final String FORMATE50="yyyyMMdd";
    public static final String FORMATE51="yyyyMMddHH";
    public static final String FORMATE52="yyyyMMddHHmm";
    public static final String FORMATE53="yyyyMMddHHmmss";
    public static final String FORMATE54="yyyyMMddHHmmssSSS";
    public static final String FORMATE55="yyyy.MM.dd";
    public static final String FORMATE61 ="dd MMMM yyyy";
    public static final String FORMATE62 ="dd MMMM yyyy HH:mm:ss";
    public static final String FORMATE63 ="dd MMMM yyyy HH:mm";
    public static final String FORMATE64 = "EEEE dd MMMM yyyy hh:mm a";

    public static final String FORMATE70="HH:mm";
    public static final String FORMATE71="HHmm";
    public static final String FORMATE72="HH";
    public static final String FORMATE73="mm";

    public static AtomicInteger atomicInteger =new AtomicInteger(100000);


    /**
     * 锁对象
     */
    private static final Object lockObj = new Object();

    /**
     * 存放不同的日期模板格式的sdf的Map
     */
    private static Map<String, ThreadLocal<SimpleDateFormat>> sdfMap = new HashMap<String, ThreadLocal<SimpleDateFormat>>();

    /**
     * 返回一个ThreadLocal的sdf,每个线程只会new一次sdf
     *
     * @param pattern 转换格式
     * @return  SimpleDateFormat
     */
    private static SimpleDateFormat getSdf(final String pattern) {
        ThreadLocal<SimpleDateFormat> tl = sdfMap.get(pattern);
        // 此处的双重判断和同步是为了防止sdfMap这个单例被多次put重复的sdf
        if (tl == null) {
            synchronized (lockObj) {
                tl = sdfMap.get(pattern);
                if (tl == null) {
                    // 只有Map中还没有这个pattern的sdf才会生成新的sdf并放入map
                    System.out.println("put new sdf of pattern " + pattern + " to map");
                    // 这里是关键,使用ThreadLocal<SimpleDateFormat>替代原来直接new SimpleDateFormat
                    tl = new ThreadLocal<SimpleDateFormat>() {
                        @Override
                        protected SimpleDateFormat initialValue() {
                            System.out.println("thread: " + Thread.currentThread() + " init pattern: " + pattern);
                            return new SimpleDateFormat(pattern);
                        }
                    };
                    sdfMap.put(pattern, tl);
                }
            }
        }
        return tl.get();
    }

    public static Date stringToDate(String dateString,String pattern,String message){
        try {
            SimpleDateFormat simpleDateFormat=new SimpleDateFormat(pattern);
            simpleDateFormat.setLenient(false);
            return simpleDateFormat.parse(dateString);
        } catch (ParseException e) {
            throw new IllegalArgumentException(message);
        }
    }


    public static Date stringToDate(String dateString,String pattern){
        try {
            SimpleDateFormat simpleDateFormat=new SimpleDateFormat(pattern);
            simpleDateFormat.setLenient(false);
            return simpleDateFormat.parse(dateString);
        } catch (ParseException e) {
            return null;
        }
    }


    public static Date stringToDate(String dateString){
        try {
            SimpleDateFormat simpleDateFormat=getSdf(DEFAULT_FORMAT);
            simpleDateFormat.setLenient(false);
            return simpleDateFormat.parse(dateString);
        } catch (ParseException e) {
            return null;
        }
    }


    public static long stringToTime(String dateString,String pattern){
        try {
            SimpleDateFormat simpleDateFormat=getSdf(pattern);
            simpleDateFormat.setLenient(false);
            Date data=simpleDateFormat.parse(dateString);
            return data.getTime();
        } catch (ParseException e) {
            throw new IllegalArgumentException(e.getMessage());
        }
    }


    public static long stringToTime(String dateString){
        try {
            SimpleDateFormat simpleDateFormat=getSdf(DEFAULT_FORMAT);
            simpleDateFormat.setLenient(false);
            Date data=simpleDateFormat.parse(dateString);
            return data.getTime();
        } catch (ParseException e) {
            throw new IllegalArgumentException(e.getMessage());
        }
    }

    public static Long stringToTimeLenient(String dateString,String pattern){
        try {
            if(AssertUtils.isNotEmpty(dateString)){
                SimpleDateFormat simpleDateFormat=getSdf(pattern);
                Date data=simpleDateFormat.parse(dateString);
                return data.getTime();
            }
            return null;
        } catch (ParseException e) {
            throw new IllegalArgumentException(e.getMessage());
        }
    }

    public static Long stringToTimeLenient(String dateString){
        try {
            if(AssertUtils.isNotEmpty(dateString)){
                Date data=getSdf(DEFAULT_FORMAT).parse(dateString);
                return data.getTime();
            }
            return null;
        } catch (ParseException e) {
            throw new IllegalArgumentException(e.getMessage());
        }

    }


    public static String dateToString(Date date) {
        return getSdf(DEFAULT_FORMAT).format(date);
    }

    public static String dateToString(Date date,String pattern) {
        return getSdf(pattern).format(date);
    }



    public static Date getTimeStrToDate(String time,String pattern){
        try{
            if(AssertUtils.isNotEmpty(time)){
                long timeL=Long.valueOf(time);
                Date date=new Date(timeL);
                SimpleDateFormat simpleDateFormat=getSdf(pattern);
                String str=simpleDateFormat.format(date);
                date=simpleDateFormat.parse(str);
                return date;
            }
            return null;
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }

    public static String timeStrToString(String timeStr,String pattern) {
        if(AssertUtils.isNotEmpty(timeStr)){
            Date date = new Date(Long.valueOf(timeStr));
            return getSdf(pattern).format(date);
        }
        return null;
    }

    public static String timeStrToString(String timeStr,String pattern,Locale lang) {
        if(AssertUtils.isNotEmpty(timeStr)){
            Date date = new Date(Long.valueOf(timeStr));
            return new SimpleDateFormat(pattern,lang).format(date);
        }
        return null;
    }

    public static String timeStrToString(Long timeStr,String pattern,Locale lang) {
        if(AssertUtils.isNotNull(timeStr)){
            Date date = new Date(timeStr);
            return new SimpleDateFormat(pattern,lang).format(date);
        }
        return null;
    }

    public static String timeStrToString(String timeStr) {
        if(AssertUtils.isNotEmpty(timeStr)){
            Date date = new Date(Long.valueOf(timeStr));
            return defaultFormat.format(date);
        }
        return null;
    }

    public static String timeStrToString(long timeStr,String pattern) {
        Date date = new Date(timeStr);
        return getSdf(pattern).format(date);
    }

    public static String timeStrToString(long timeStr) {
        Date date = new Date(timeStr);
        return getSdf(DEFAULT_FORMAT).format(date);
    }

    public static String timeStrToString(Long timeStr,String pattern) {
        if(AssertUtils.isNotNull(timeStr)){
            Date date = new Date(timeStr);
            return getSdf(pattern).format(date);
        }
        return null;
    }

    public static String timeStrToString(Long timeStr) {
        if(AssertUtils.isNotNull(timeStr)){
            Date date = new Date(timeStr);
            return getSdf(DEFAULT_FORMAT).format(date);
        }
       return null;
    }

    public static Long timeToTime(long timeL,String pattern) {
        try{
            Date date=new Date(timeL);
            SimpleDateFormat simpleDateFormat=getSdf(pattern);
            String str=simpleDateFormat.format(date);
            date=simpleDateFormat.parse(str);
            return date.getTime();
        }catch (Exception e){
            e.printStackTrace();
            return 0L;
        }
    }

    public static Long timeToDateTime(long timeL) {
        try{
            Date date=new Date(timeL);
            SimpleDateFormat simpleDateFormat=getSdf(DEFAULT_FORMAT);
            String str=simpleDateFormat.format(date);
            date=simpleDateFormat.parse(str);
            return date.getTime();
        }catch (Exception e){
            e.printStackTrace();
            return 0L;
        }
    }

    /**
     *
     * @param dateString 时间字符串
     * @param returnType
     * @return
     */
    public  static long dateInterval(String dateString,String pattern ,long returnType){
        long  interval = 0;
        try {
            Date startdate  =  stringToDate(dateString,pattern);
            long startMills = startdate.getTime();
            long  nowMills = System.currentTimeMillis();
            interval = (nowMills-startMills)/(returnType*1000);
        }catch (Exception e){

        }
        return interval;
    }

    public static Date addDays(Date paramDate, int paramInt)
            throws Exception {
        Calendar localCalendar = Calendar.getInstance();
        //localCalendar.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
        localCalendar.setTime(paramDate);
        int i = localCalendar.get(Calendar.DAY_OF_YEAR);
        localCalendar.set(Calendar.DAY_OF_YEAR, i + paramInt);
        return localCalendar.getTime();
    }


    public static Date addYears(Date paramDate, int paramInt)
            throws Exception {
        Calendar localCalendar = Calendar.getInstance();
        //localCalendar.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
        localCalendar.setTime(paramDate);
        int i = localCalendar.get(Calendar.YEAR);
        localCalendar.set(Calendar.YEAR, i + paramInt);
        return localCalendar.getTime();
    }
    /**
     *  日期字符串  加天数
     * @param strDate 字符串日期
     * @param paramInt  加多少天
     * @return
     * @throws Exception
     */
    public static String addStringDays(String strDate, int paramInt){
        String returnStr=strDate;
        try {
            Date date = DateUtils.stringToDate(strDate,"yyyy-MM-dd");
            Calendar   calendar   =   Calendar.getInstance();
            //calendar.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
            calendar.setTime(date);
            calendar.add(Calendar.DATE,paramInt);//把日期往后增加一天.整数往后推,负数往前移动
            returnStr=DateUtils.dateToString(calendar.getTime());
        }catch (Exception e){


        }
        return  returnStr;
    }

    /**
     *  日期字符串  加天数
     * @param time 时间戳
     * @param paramInt  加多少年
     * @return
     * @throws Exception
     */
    public static long addStringYearsRT(long time, int paramInt){
       long returnTime=-1;
        try {
            Date date = new Date(time);
            Calendar   calendar   =   Calendar.getInstance();
            //calendar.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
            calendar.setTime(date);
            calendar.add(Calendar.YEAR,paramInt);//把日期往后增加N年.整数往后推,负数往前移动
            returnTime=calendar.getTimeInMillis();
        }catch (Exception e){
        }
        return  returnTime;
    }

    /**
     *  日期字符串  加天数
     * @param time 时间戳
     * @param paramInt  加多少年
     * @return
     * @throws Exception
     */
    public static long addStringMonthRT(long time, int paramInt){
        long returnTime=-1;
        try {
            Date date = new Date(time);
            Calendar   calendar   =   Calendar.getInstance();
            //calendar.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
            calendar.setTime(date);
            calendar.add(Calendar.MONTH,paramInt);//把日期往后增加N年.整数往后推,负数往前移动
            returnTime=calendar.getTimeInMillis();
        }catch (Exception e){
        }
        return  returnTime;
    }

    /**
     *  日期字符串  加天数
     * @param time 时间戳
     * @param paramInt  加多少年
     * @return
     * @throws Exception
     */
    public static long addStringDayRT(long time, int paramInt){
        long returnTime=-1;
        try {
            Date date = new Date(time);
            Calendar   calendar   =   Calendar.getInstance();
            //calendar.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
            calendar.setTime(date);
            calendar.add(Calendar.DAY_OF_MONTH,paramInt);//把日期往后增加N年.整数往后推,负数往前移动
            returnTime=calendar.getTimeInMillis();
            //Date date = new Date(time+(paramInt*24*60*60*1000));
            //returnTime=date.getTime();
        }catch (Exception e){
        }
        return  returnTime;
    }

    /**
     * 添加小时
     * @param paramDate
     * @param paramInt
     * @return
     * @throws Exception
     */
    public static Date addHours(Date paramDate, int paramInt) throws Exception {
        Calendar localCalendar = Calendar.getInstance();
        localCalendar.setTime(paramDate);
        int i = localCalendar.get(Calendar.HOUR_OF_DAY);
        localCalendar.set(Calendar.HOUR_OF_DAY, i + paramInt);
        return localCalendar.getTime();
    }

    /**
     *  日期字符串  加天数
     * @param time 时间戳
     * @param paramInt  加多少年
     * @return
     * @throws Exception
     */
    public static long addStringDayRT(long time, long paramInt){
        long returnTime=-1;
        try {
            //Date date = new Date(time+(paramInt*24*60*60*1000));
            //returnTime=date.getTime();
            Date date = new Date(time);
            Calendar   calendar   =   Calendar.getInstance();
            //calendar.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
            calendar.setTime(date);
            calendar.add(Calendar.DAY_OF_MONTH,(int)paramInt);//把日期往后增加N年.整数往后推,负数往前移动
            returnTime=calendar.getTimeInMillis();
        }catch (Exception e){
        }
        return  returnTime;
    }

    /**
     *  日期字符串  减天数
     * @param time 时间戳
     * @param paramInt  加多少年
     * @return
     * @throws Exception
     */
    public static long minusStringDayRT(long time, long paramInt){
        long returnTime=-1;
        try {
            Date date = new Date(time-(paramInt*24*60*60*1000));
            returnTime=date.getTime();
        }catch (Exception e){
        }
        return  returnTime;
    }

    /**
     *  返回当天时间戳的０点
     * @param time 时间戳
     * @return
     * @throws Exception
     */
    public static long timeToTimeLow(long time){
        long returnTime=-1;
        try {
            Date date = new Date(time);
            Calendar   calendar   = Calendar.getInstance();
            //calendar.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
            calendar.setTime(date);
            calendar.set(Calendar.HOUR_OF_DAY,0);
            calendar.set(Calendar.MINUTE,0);
            calendar.set(Calendar.SECOND,0);
            calendar.set(Calendar.MILLISECOND,000);
            returnTime=calendar.getTimeInMillis();
        }catch (Exception e){
        }
        return  returnTime;
    }


    /**
     *  返回当天时间戳的23:59:59点
     * @param time 时间戳
     * @return
     * @throws Exception
     */
    public static long timeToTimeTop(long time){
        long returnTime=-1;
        try {
            Date date = new Date(time);
            Calendar   calendar   = Calendar.getInstance();
            //calendar.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
            calendar.setTime(date);
            calendar.set(Calendar.HOUR_OF_DAY,23);
            calendar.set(Calendar.MINUTE,59);
            calendar.set(Calendar.SECOND,59);
            calendar.set(Calendar.MILLISECOND,999);
            returnTime=calendar.getTimeInMillis();
        }catch (Exception e){
        }
        return  returnTime;
    }


    /**
     *  获取当前年月
     * @return
     * @throws Exception
     */
    public static String  getCurrentYearMonth(){
        String returnTime="";
        try {
          returnTime = new SimpleDateFormat("yyyyMM").format(new Date());
        }catch (Exception e){
        }
        return  returnTime;
    }

    /**
     *  获取上月年月
     * @return
     * @throws Exception
     */
    public static String  getLastYearMonth(){
        String returnTime="";
        try {
            Calendar c = Calendar.getInstance();
            //c.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
            c.add(Calendar.MONTH, -1);

            returnTime = new SimpleDateFormat("yyyyMM").format(c.getTime());
        }catch (Exception e){
        }
        return  returnTime;
    }


    /**
     *  获取当前年月的上月　-1,上上月　-2
     * @return
     * @throws Exception
     */
    public static String  getYearMonth(int i){
        String returnTime="";
        try {
            Calendar c = Calendar.getInstance();
            //c.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
            c.add(Calendar.MONTH, i);
            returnTime = new SimpleDateFormat("yyyyMM").format(c.getTime());
        }catch (Exception e){
        }
        return  returnTime;
    }



    public static String getTimeYearMonth(long time) {
        String returnTime = "";

        try {
            Date date = new Date(time);
            Calendar c = Calendar.getInstance();
            c.setTime(date);
            returnTime = (new SimpleDateFormat("yyyyMM")).format(c.getTime());
        } catch (Exception var3) {
            ;
        }

        return returnTime;
    }


    /**
     *  获取当前日期的时间戳
     * @return
     * @throws Exception
     */
    public static Long  getCurrentDateToTime(){
        Long returnTime=0L;
        try {
            Date date=new Date();
            SimpleDateFormat simpleDateFormat=getSdf(DEFAULT_FORMAT);
            String str=simpleDateFormat.format(date);
            date=simpleDateFormat.parse(str);
            return date.getTime();
        }catch (Exception e){
        }
        return  returnTime;
    }


    /**
     *  获取当前时间的时间戳
     * @return
     * @throws Exception
     */
    public static Long  getCurrentTime(){
        return  System.currentTimeMillis();
    }

    /**
     * 当前季度的开始年月
     * @return
     */
    public static String getQuarterStartYearMonth(Date date) {
        Calendar c = Calendar.getInstance();
        //c.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
        c.setTime(date);
        int currentMonth = c.get(Calendar.MONTH) + 1;
        Date now = null;
        try {
            if(currentMonth >= 1 && currentMonth <= 3) {
                c.set(Calendar.MONTH, 0);
            }else if (currentMonth >= 4 && currentMonth <= 6) {
                c.set(Calendar.MONTH, 3);
            }else if (currentMonth >= 7 && currentMonth <= 9) {
                c.set(Calendar.MONTH, 6);
            }else if (currentMonth >= 10 && currentMonth <= 12) {
                c.set(Calendar.MONTH, 9);
            }
            c.set(Calendar.DATE, 1);
            return getSdf(FORMATE16).format(c.getTime());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     *  获取业绩日期
     * @return
     * @throws Exception
     */
    public static Long  getBusinessDate(String bizDay){
        try {
            String day  = DateUtils.getCurrentYearMonth()+bizDay;
            return DateUtils.stringToTime(day,"yyyyMMdd");
        }catch (Exception e){
        }
        return  0L;
    }




    /**
     * 获取当月的第一天 0.0.0
     * @param yearMonth
     * @param pattern 格式化编码
     * @return
     */
    public static long getThisMonthFirstDay(String yearMonth,String pattern){
        if(!AssertUtils.isNotEmpty(pattern)){
            pattern = "yyyyMM";
        }
        long time = DateUtils.stringToTime(yearMonth, pattern);
        Calendar c = Calendar.getInstance();
        c.setTimeInMillis(time);
        c.add(2, 0);
        c.set(5, 1);
        c.set(11, 0);
        c.set(12, 0);
        c.set(13, 0);
        c.set(14, 0);
        return c.getTimeInMillis();
    }

    /**
     * 获取当月的一天 0.0.0
     * @param yearMonth
     * @return
     */
    public static long getThisMonthFirstDay(String yearMonth) {
        return getThisMonthLastDay(yearMonth,null);
    }

    /**
     * 获取当月的最后一天 24.59.59
     * @param yearMonth
     * @param pattern 格式化编码
     * @return
     */
    public static long getThisMonthLastDay(String yearMonth,String pattern){
        if(!AssertUtils.isNotEmpty(pattern)){
            pattern = "yyyyMM";
        }
        long time =  DateUtils.stringToTime(yearMonth, pattern);
        Calendar c = Calendar.getInstance();
        c.setTimeInMillis(time);
        c.set(5, c.getActualMaximum(5));
        c.set(11, 23);
        c.set(12, 59);
        c.set(13, 59);
        c.set(14, 999);
        return c.getTimeInMillis();
    }

    /**
     * 获取当月的最后一天 24.59.59
     * @param yearMonth
     * @return
     */
    public static long getThisMonthLastDay(String yearMonth) {
        return getThisMonthLastDay(yearMonth,null);
    }


    /**
     * 获取某年第一天日期
     *
     * @param year 年份
     * @return Long
     */
    public static Long getYearFirstDay(int year) {
        Calendar calendar = Calendar.getInstance();
        //calendar.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
        calendar.clear();
        calendar.set(Calendar.YEAR, year);
        Date currYearFirst = calendar.getTime();
        return currYearFirst.getTime();
    }

    /**
     * 获取某年最后一天日期
     *
     * @param year 年份
     * @return Long
     */
    public static Long getYearLastDay(int year) {
        Calendar calendar = Calendar.getInstance();
        //calendar.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
        calendar.clear();
        calendar.set(Calendar.YEAR, year);
        calendar.roll(Calendar.DAY_OF_YEAR, -1);
        Date currYearLast = calendar.getTime();
        return currYearLast.getTime() + (24 * 60 * 60 * 1000 - 1);
    }

    /**
     * 获取本月第一天时间戳
     *
     * @return long
     */
    public static long getThisMonthFirstDay() {
        Calendar c = Calendar.getInstance();
        //c.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
        c.add(Calendar.MONTH, 0);
        //设置为1号,当前日期既为本月第一天
        c.set(Calendar.DAY_OF_MONTH, 1);
        c.set(Calendar.HOUR_OF_DAY, 0);
        c.set(Calendar.MINUTE, 0);
        c.set(Calendar.SECOND, 0);
        c.set(Calendar.MILLISECOND, 0);
        return c.getTimeInMillis();
    }

    /**
     * 获取本月最后一天时间戳
     *
     * @return long
     */
    public static long getThisMonthLastDay() {
        Calendar c = Calendar.getInstance();
        //c.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
        c.set(Calendar.DAY_OF_MONTH, c.getActualMaximum(Calendar.DAY_OF_MONTH));
        c.set(Calendar.HOUR_OF_DAY, 23);
        c.set(Calendar.MINUTE, 59);
        c.set(Calendar.SECOND, 59);
        c.set(Calendar.MILLISECOND, 999);
        return c.getTimeInMillis();
    }


    /**
     * 获取指定年月的最开始时间
     *
     * @return long
     */
    public static long getThisMonthFirstDay(long time) {
        Calendar c = Calendar.getInstance();
        //c.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
        c.setTimeInMillis(time);
        c.add(Calendar.MONTH, 0);
        //设置为1号,当前日期既为本月第一天
        c.set(Calendar.DAY_OF_MONTH, 1);
        c.set(Calendar.HOUR_OF_DAY, 0);
        c.set(Calendar.MINUTE, 0);
        c.set(Calendar.SECOND, 0);
        c.set(Calendar.MILLISECOND, 0);
        return c.getTimeInMillis();
    }

    /**
     * 获取指定年月的最后时间
     *
     * @return long
     */
    public static long getThisMonthLastDay(long time) {
        Calendar c = Calendar.getInstance();
        //c.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
        c.setTimeInMillis(time);
        c.set(Calendar.DAY_OF_MONTH, c.getActualMaximum(Calendar.DAY_OF_MONTH));
        c.set(Calendar.HOUR_OF_DAY, 23);
        c.set(Calendar.MINUTE, 59);
        c.set(Calendar.SECOND, 59);
        c.set(Calendar.MILLISECOND, 999);
        return c.getTimeInMillis();
    }


    /**
     * 获取上个月第一天时间戳
     *
     * @return long
     */
    public static long getLastMonthFirstDay() {
        //获取当前日期
        Calendar c = Calendar.getInstance();
        //c.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
        c.add(Calendar.MONTH, -1);
        //设置为1号,当前日期既为本月第一天
        c.set(Calendar.DAY_OF_MONTH, 1);
        c.set(Calendar.HOUR_OF_DAY, 0);
        c.set(Calendar.MINUTE, 0);
        c.set(Calendar.SECOND, 0);
        c.set(Calendar.MILLISECOND, 0);
        return c.getTimeInMillis();
    }

    /**
     * 获取上个月最后一天时间戳
     *
     * @return long
     */
    public static long getLastMonthLastDay() {
        //获取前月的最后一天
        Calendar c = Calendar.getInstance();
        //c.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
        //设置为1号,当前日期既为本月第一天
        c.set(Calendar.DAY_OF_MONTH, 0);
        c.set(Calendar.HOUR_OF_DAY, 23);
        c.set(Calendar.MINUTE, 59);
        c.set(Calendar.SECOND, 59);
        c.set(Calendar.MILLISECOND, 999);
        return c.getTimeInMillis();
    }

    /**
     *  获取前天
     * @return Long
     */
    public static Long getYesterDay() {
        Calendar calendar = Calendar.getInstance();
        //calendar.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
        calendar.setTime(new Date());
        calendar.add(Calendar.DATE, -1);
        return calendar.getTime().getTime();
    }

    /**
     *  获取当前月的第一天
     * @return Long
     */
    public static Long getCurrentMonthFirstDay() {
        Calendar cale = Calendar.getInstance();
        //cale.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
        cale.add(Calendar.MONTH, 0);
        cale.set(Calendar.DAY_OF_MONTH, 1);
        return cale.getTime().getTime();
    }

    /**
     *  获取当前月的最后一天
     * @return Long
     */
    public static Long getCurrentMonthLastDay() {
        Calendar cale = Calendar.getInstance();
        //cale.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
        cale = Calendar.getInstance();
        cale.add(Calendar.MONTH, 1);
        cale.set(Calendar.DAY_OF_MONTH, 0);
        return cale.getTime().getTime();
    }

    /**
     *  获取当月的天数
     * @return Long
     */
    public static int getCurrentMonthDays() {
        Calendar a = Calendar.getInstance();
        //a.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
        a.set(Calendar.DATE, 1);
        a.roll(Calendar.DATE, -1);
        return a.get(Calendar.DATE);
    }

    /**
     * 根据年月获取上年月
     * @param yearMonth 年月
     * @return  String
     */
    public static String getLastMonth(String yearMonth){
        try {
            SimpleDateFormat format =getSdf(FORMATE16);
            Calendar c = Calendar.getInstance();
            //c.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
            c.setTime(format.parse(yearMonth));
            c.add(Calendar.MONTH, -1);
            return format.format(c.getTime());
        }catch (Exception e){
            return null;
        }
    }

    /**
     * 日期字符串格式转换
     * @param datestr 年月　日期字符串
     * @return  String
     */
    public static String dateStringFormatTransfer(String datestr,String soureFormat,String targetFormat){
        try {
            if(AssertUtils.isNotEmpty(datestr)){
                SimpleDateFormat format = getSdf(soureFormat);
                SimpleDateFormat format2 = getSdf(targetFormat);
                format2.format(format.parse(datestr));
                return format2.format(format.parse(datestr));
            }
        }catch (Exception e){
            return null;
        }
        return null;
    }


    //由出生日期获得年龄
    public static int getAgeYear(Date birthDay) throws Exception {
        Calendar cal = Calendar.getInstance();
        //cal.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
        if (cal.before(birthDay)) {
            throw new IllegalArgumentException(
                    "The birthDay is before Now.It's unbelievable!");
        }
        int yearNow = cal.get(Calendar.YEAR);
        int monthNow = cal.get(Calendar.MONTH);
        int dayOfMonthNow = cal.get(Calendar.DAY_OF_MONTH);
        cal.setTime(birthDay);

        int yearBirth = cal.get(Calendar.YEAR);
        int monthBirth = cal.get(Calendar.MONTH);
        int dayOfMonthBirth = cal.get(Calendar.DAY_OF_MONTH);

        int age = yearNow - yearBirth;

        if (monthNow <= monthBirth) {
            if (monthNow == monthBirth) {
                if (dayOfMonthNow < dayOfMonthBirth) age--;
            }else{
                age--;
            }
        }
        return age;
    }

    //由出生日期获得年龄
    public static int getAgeMonth(Date birthDay) throws Exception {
        Calendar cal = Calendar.getInstance();
        //cal.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
        if (cal.before(birthDay)) {
            throw new IllegalArgumentException(
                    "The birthDay is before Now.It's unbelievable!");
        }
        int yearNow = cal.get(Calendar.YEAR);
        int monthNow = cal.get(Calendar.MONTH);
        int dayOfMonthNow = cal.get(Calendar.DAY_OF_MONTH);
        cal.setTime(birthDay);

        int yearBirth = cal.get(Calendar.YEAR);
        int monthBirth = cal.get(Calendar.MONTH);
        int dayOfMonthBirth = cal.get(Calendar.DAY_OF_MONTH);

        int age = ((yearNow - yearBirth) * 12) + monthBirth;
        if (monthNow == monthBirth) {
            if (dayOfMonthNow < dayOfMonthBirth) {age--;}
        }else{
            age--;
        }
        return age;
    }

    //由出生年月得到天数
    public static int getAgeDay(Date birthDay)
    {
        int days = (int) ((System.currentTimeMillis() - birthDay.getTime()) / (1000*3600*24));
        return days;
    }

    //由投保日期获取保单年度
    public static int getPolicyYear(Date applyDate) throws Exception {
        Calendar cal = Calendar.getInstance();
        //cal.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
        if (cal.before(applyDate)) {
            throw new IllegalArgumentException(
                    "The birthDay is before Now.It's unbelievable!");
        }
        int yearNow = cal.get(Calendar.YEAR);
        int monthNow = cal.get(Calendar.MONTH);
        int dayOfMonthNow = cal.get(Calendar.DAY_OF_MONTH);
        cal.setTime(applyDate);

        int yearBirth = cal.get(Calendar.YEAR);
        int monthBirth = cal.get(Calendar.MONTH);
        int dayOfMonthBirth = cal.get(Calendar.DAY_OF_MONTH);

        int age = yearNow - yearBirth+1;

        if (monthNow <= monthBirth) {
            if (monthNow == monthBirth) {
                if (dayOfMonthNow < dayOfMonthBirth) age--;
            }else {
                age--;
            }
        }
        return age;
    }

    //由投保日期获得保单月度
    public static int getPolicyMonth(Date applyDate) throws Exception {
        Calendar cal = Calendar.getInstance();
        //cal.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
        if (cal.before(applyDate)) {
            throw new IllegalArgumentException(
                    "The birthDay is before Now.It's unbelievable!");
        }
        int yearNow = cal.get(Calendar.YEAR);
        int monthNow = cal.get(Calendar.MONTH);
        int dayOfMonthNow = cal.get(Calendar.DAY_OF_MONTH);
        cal.setTime(applyDate);

        int yearBirth = cal.get(Calendar.YEAR);
        int monthBirth = cal.get(Calendar.MONTH);
        int dayOfMonthBirth = cal.get(Calendar.DAY_OF_MONTH);

        int age = ((yearNow - yearBirth) * 12) + (monthNow-monthBirth)+1;
        if (monthNow == monthBirth) {
            if (dayOfMonthNow < dayOfMonthBirth) {age--;}
        }
        return age;
    }


    //由投保日期获得当前年度的保单月度
    public static int getCurrentYearPolicyMonth(Date applyDate) throws Exception {
        Long currentApplyTime = DateUtils.addStringYearsRT(applyDate.getTime(),getPolicyYear(applyDate)-1);
        Date currentApplyDate = StringUtil.getDateAll(currentApplyTime+"");
        System.out.println("currentApplyDate:"+DateUtils.dateToString(currentApplyDate));
        Calendar cal = Calendar.getInstance();
        //cal.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
        if (cal.before(currentApplyDate)) {
            throw new IllegalArgumentException(
                    "The birthDay is before Now.It's unbelievable!");
        }
        int yearNow = cal.get(Calendar.YEAR);
        int monthNow = cal.get(Calendar.MONTH);
        int dayOfMonthNow = cal.get(Calendar.DAY_OF_MONTH);
        cal.setTime(currentApplyDate);

        int yearBirth = cal.get(Calendar.YEAR);
        int monthBirth = cal.get(Calendar.MONTH);
        int dayOfMonthBirth = cal.get(Calendar.DAY_OF_MONTH);

        int age = ((yearNow - yearBirth) * 12) + (monthNow-monthBirth)+1;
        if (dayOfMonthNow < dayOfMonthBirth) {age--;}
        return age;
    }

    //由投保日期获得当前您度投保天数
    public static int getCurrentYearPolicyDay(Date applyDate) throws Exception
    {
        Long currentApplyTime = DateUtils.addStringYearsRT(applyDate.getTime(),getPolicyYear(applyDate)-1);
        Date currentApplyDate = StringUtil.getDateAll(currentApplyTime+"");
        int days = (int) ((System.currentTimeMillis() - currentApplyDate.getTime()) / (1000*3600*24))+1;
        return days;
    }

    /**
     * 保单年度已过的天数
     * @param applyDate 2
     * @param t 代表保单经过的年度数
     * @return
     * @throws Exception
     */
    //由投保日期获得当前您度投保天数
    public static int getCurrentYearPolicyPassedDay(Date applyDate,int t) throws Exception
    {
        Long currentApplyTime = DateUtils.addStringYearsRT(applyDate.getTime(),t);
        Date currentApplyDate = StringUtil.getDateAll(currentApplyTime+"");
        //当前时间大于 当前投保日期加上一年．表示已经过了当前年度了
        if(System.currentTimeMillis()>DateUtils.addStringYearsRT(currentApplyDate.getTime(),1)){
            return 365;
        }else {
            int days = (int) ((System.currentTimeMillis() - currentApplyDate.getTime()) / (1000*3600*24));
            return days;
        }
    }

    /**
     * 保单年度已过的天数
     * @param applyDate 2
     * @param t 代表保单经过的年度数
     * @return
     * @throws Exception
     */
    //由投保日期获得当前您度投保天数
    public static int getCurrentYearPolicyPassedDay(Date applyDate,long acceptDate, int t) throws Exception
    {
        Long currentApplyTime = DateUtils.addStringYearsRT(applyDate.getTime(),t);
        Date currentApplyDate = StringUtil.getDateAll(currentApplyTime+"");
        //当前时间大于 当前投保日期加上一年．表示已经过了当前年度了
        if(acceptDate>DateUtils.addStringYearsRT(currentApplyDate.getTime(),1)){
            return 365;
        }else {
            int days = (int) ((acceptDate - currentApplyDate.getTime()) / (1000*3600*24));
            return days;
        }
    }

    //由投保日期获得月投保天数
    public static int getPolicyDay(Date applyDate)
    {
        int days = (int) ((System.currentTimeMillis() - applyDate.getTime()) / (1000*3600*24))+1;
        return days;
    }


    /**
     * 两个日期相差
     * @param date1 <yyyyMM>
     * @param date2 <yyyyMM>
     * @return int
     */
    public static int getMonthSpace(String date1, String date2){
        try{
            int result = 0;

            SimpleDateFormat sdf = getSdf("yyyyMM");

            Calendar c1 = Calendar.getInstance();
            Calendar c2 = Calendar.getInstance();

            c1.setTime(sdf.parse(date1));
            c2.setTime(sdf.parse(date2));

            result = c2.get(Calendar.MONTH) - c1.get(Calendar.MONTH);
            int month = (c2.get(Calendar.YEAR) - c1.get(Calendar.YEAR)) * 12;

            return Math.abs(month + result);
        }catch (Exception e){
            e.printStackTrace();
        }
        return 1;
    }

    //获取当天的开始时间
    public static Date getDayBegin() {
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);
        return cal.getTime();
    }
    //获取当天的结束时间
    public static Date getDayEnd() {
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.HOUR_OF_DAY, 23);
        cal.set(Calendar.MINUTE, 59);
        cal.set(Calendar.SECOND, 59);
        return cal.getTime();
    }
    //获取昨天的开始时间
    public static Date getBeginDayOfYesterday() {
        Calendar cal = Calendar.getInstance();
        cal.setTime(getDayBegin());
        cal.add(Calendar.DAY_OF_MONTH, -1);
        return cal.getTime();
    }
    //获取昨天的结束时间
    public static Date getEndDayOfYesterDay() {
        Calendar cal = Calendar.getInstance();
        cal.setTime(getDayEnd());
        cal.add(Calendar.DAY_OF_MONTH, -1);
        return cal.getTime();
    }
    //获取明天的开始时间
    public static Date getBeginDayOfTomorrow() {
        Calendar cal = Calendar.getInstance();
        cal.setTime(getDayBegin());
        cal.add(Calendar.DAY_OF_MONTH, 1);

        return cal.getTime();
    }
    //获取明天的结束时间
    public static Date getEndDayOfTomorrow() {
        Calendar cal = Calendar.getInstance();
        cal.setTime(getDayEnd());
        cal.add(Calendar.DAY_OF_MONTH, 1);
        return cal.getTime();
    }

    /**
     * 获取年龄
     * @param birthDay 生日
     * @param culcationDay
     * @return
     * @throws Exception
     */
    public static int getAgeYear(Date birthDay,Date culcationDay) throws Exception {
        Calendar cal = Calendar.getInstance();
        cal.setTime(culcationDay);
        if (cal.before(birthDay)) {
            throw new IllegalArgumentException("The birthDay is before Now.It's unbelievable!");
        } else {
            int yearNow = cal.get(1);
            int monthNow = cal.get(2);
            int dayOfMonthNow = cal.get(5);
            cal.setTime(birthDay);
            int yearBirth = cal.get(1);
            int monthBirth = cal.get(2);
            int dayOfMonthBirth = cal.get(5);
            int age = yearNow - yearBirth;
            if (monthNow <= monthBirth) {
                if (monthNow == monthBirth) {
                    if (dayOfMonthNow < dayOfMonthBirth) {
                        --age;
                    }
                } else {
                    --age;
                }
            }

            return age;
        }
    }


    /**
     * 根据日期格式获取流程，工号..通用方法
     */
    /**
     * 根据日期格式获取通用工号
     * @param prefix　前缀
     * @param suffix　后缀
     * @param format　时间字符串格式
     * @param isTimestamp 是否时间戳
     * @return  String
     */
    public static String getJobNumberByTime(String prefix,String suffix,String format,boolean isTimestamp) {
        String number =null;
        if(isTimestamp){
            number=System.currentTimeMillis()+"";
            if(AssertUtils.isNotEmpty(prefix)){
                number =prefix+number;
            }
            if(AssertUtils.isNotEmpty(suffix)){
                number=number+suffix;
            }
        }else {
            Calendar c = Calendar.getInstance();
            c.setTime(new Date());
            number = (getSdf(format)).format(c.getTime());
            if(AssertUtils.isNotEmpty(prefix)){
                number =prefix+number;
            }
            if(AssertUtils.isNotEmpty(suffix)){
                number=number+suffix;
            }
        }
        return number;
    }


    /**
     * 根据日期格式获取流程，工号..通用方法
     */
    /**
     * 根据日期格式获取通用工号
     * @param prefix　前缀
     * @param suffix　后缀
     * @param format　时间字符串格式
     * @param isTimestamp 是否时间戳
     * @return  String
     */
    public static String getJobNumberByTime2(String prefix,String suffix,String format,boolean isTimestamp) {
        String number =null;
        if(isTimestamp){
            number=System.currentTimeMillis()+"";
            if(AssertUtils.isNotEmpty(prefix)){
                number =prefix+number;
            }
            if(AssertUtils.isNotEmpty(suffix)){
                number=number+suffix;
            }
        }else {
            Calendar c = Calendar.getInstance();
            c.setTime(new Date());
            number = (getSdf(format)).format(c.getTime());
            if(AssertUtils.isNotEmpty(prefix)){
                number =prefix+number;
            }
            if(AssertUtils.isNotEmpty(suffix)){
                number=number+suffix;
            }
        }

        int a = atomicInteger.incrementAndGet();
        if(a>=999999){
            atomicInteger.set(100000);
        }
        number=number+a;
        return number;
    }
    /**
     * 间隔天数
     * @param startTime
     * @param endTime
     * @return
     */
    public static long intervalDay(long startTime, long endTime){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date(startTime));
        LocalDate startLocalDate = LocalDate.of(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH)+1, calendar.get(Calendar.DAY_OF_MONTH));
        calendar.setTime(new Date(endTime));
        LocalDate endLocalDate = LocalDate.of(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH)+1, calendar.get(Calendar.DAY_OF_MONTH));
        return startLocalDate.until(endLocalDate, ChronoUnit.DAYS);
    }

    /**
     * 间隔月数
     * @param startTime
     * @param endTime
     * @return
     */
    public static long intervalMonth(long startTime, long endTime){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date(startTime));
        LocalDate startLocalDate = LocalDate.of(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH)+1, calendar.get(Calendar.DAY_OF_MONTH));
        calendar.setTime(new Date(endTime));
        LocalDate endLocalDate = LocalDate.of(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH)+1, calendar.get(Calendar.DAY_OF_MONTH));
        return startLocalDate.until(endLocalDate, ChronoUnit.MONTHS);
    }

    /**
     * 间隔年数
     * @param startTime
     * @param endTime
     * @return
     */
    public static long intervalYear(long startTime, long endTime){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date(startTime));
        LocalDate startLocalDate = LocalDate.of(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH)+1, calendar.get(Calendar.DAY_OF_MONTH));
        calendar.setTime(new Date(endTime));
        LocalDate endLocalDate = LocalDate.of(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH)+1, calendar.get(Calendar.DAY_OF_MONTH));
        return startLocalDate.until(endLocalDate, ChronoUnit.YEARS);
    }

    /**
     * 间隔
     * @param startTime
     * @param endTime
     * @param type
     * @return
     */
    public static long interval(long startTime, long endTime, int type){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date(startTime));
        LocalDate startLocalDate = LocalDate.of(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH)+1, calendar.get(Calendar.DAY_OF_MONTH));
        calendar.setTime(new Date(endTime));
        LocalDate endLocalDate = LocalDate.of(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH)+1, calendar.get(Calendar.DAY_OF_MONTH));
        switch (type) {
            case Calendar.YEAR:
                return startLocalDate.until(endLocalDate, ChronoUnit.YEARS);
            case Calendar.MONTH:
                return startLocalDate.until(endLocalDate, ChronoUnit.MONTHS);
            case Calendar.WEEK_OF_YEAR:
                return startLocalDate.until(endLocalDate, ChronoUnit.WEEKS);
            case Calendar.DAY_OF_YEAR:
                return startLocalDate.until(endLocalDate, ChronoUnit.DAYS);
            default:
                return 0L;
        }
    }
    public static String getWeek(Long time) {
        if(!AssertUtils.isNotNull(time)){
            return "";
        }
        String[] weeks = {"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};
        Calendar cal = Calendar.getInstance();
        cal.setTime(new Date(time));
        int week_index = cal.get(Calendar.DAY_OF_WEEK) - 1;
        if (week_index < 0) {
            week_index = 0;
        }
        return weeks[week_index];
    }


    public static Date addAndSubtract(int month) {
        return addAndSubtract(month,null);
    }

    public static Date addAndSubtract(int month,Long dateLong) {
        Calendar c = Calendar.getInstance();

        if(AssertUtils.isNotNull(dateLong)){
            c.setTime(new Date(dateLong));
        }else {
            c.setTime(new Date());
        }
        c.add(Calendar.MONTH, month);
        return c.getTime();
    }

//
//    public static void main(String[] args) {
//        System.out.println("");
//    }
//

//    //获取本周的开始时间
//    public static Date getBeginDayOfWeek() {
//        Date date = new Date();
//        if (date == null) {
//            return null;
//        }
//        Calendar cal = Calendar.getInstance();
//        cal.setTime(date);
//        int dayofweek = cal.get(Calendar.DAY_OF_WEEK);
//        if (dayofweek == 1) {
//            dayofweek += 7;
//        }
//        cal.add(Calendar.DATE, 2 - dayofweek);
//        return getDayStartTime(cal.getTime());
//    }
//    //获取本周的结束时间
//    public static Date getEndDayOfWeek(){
//        Calendar cal = Calendar.getInstance();
//        cal.setTime(getBeginDayOfWeek());
//        cal.add(Calendar.DAY_OF_WEEK, 6);
//        Date weekEndSta = cal.getTime();
//        return getDayEndTime(weekEndSta);
//    }
//    //获取本月的开始时间
//    public static Date getBeginDayOfMonth() {
//        Calendar calendar = Calendar.getInstance();
//        calendar.set(getNowYear(), getNowMonth() - 1, 1);
//        return getDayStartTime(calendar.getTime());
//    }
//    //获取本月的结束时间
//    public static Date getEndDayOfMonth() {
//        Calendar calendar = Calendar.getInstance();
//        calendar.set(getNowYear(), getNowMonth() - 1, 1);
//        int day = calendar.getActualMaximum(5);
//        calendar.set(getNowYear(), getNowMonth() - 1, day);
//        return getDayEndTime(calendar.getTime());
//    }
//    //获取本年的开始时间
//    public static java.util.Date getBeginDayOfYear() {
//        Calendar cal = Calendar.getInstance();
//        cal.set(Calendar.YEAR, getNowYear());
//        // cal.set
//        cal.set(Calendar.MONTH, Calendar.JANUARY);
//        cal.set(Calendar.DATE, 1);
//
//        return getDayStartTime(cal.getTime());
//    }
//    //获取本年的结束时间
//    public static java.util.Date getEndDayOfYear() {
//        Calendar cal = Calendar.getInstance();
//        cal.set(Calendar.YEAR, getNowYear());
//        cal.set(Calendar.MONTH, Calendar.DECEMBER);
//        cal.set(Calendar.DATE, 31);
//        return getDayEndTime(cal.getTime());
//    }
//    //获取某个日期的开始时间
//    public static Timestamp getDayStartTime(Date d) {
//        Calendar calendar = Calendar.getInstance();
//        if(null != d) calendar.setTime(d);
//        calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH),    calendar.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
//        calendar.set(Calendar.MILLISECOND, 0);
//        return new Timestamp(calendar.getTimeInMillis());
//    }
//    //获取某个日期的结束时间
//    public static Timestamp getDayEndTime(Date d) {
//        Calendar calendar = Calendar.getInstance();
//        if(null != d) calendar.setTime(d);
//        calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH),    calendar.get(Calendar.DAY_OF_MONTH), 23, 59, 59);
//        calendar.set(Calendar.MILLISECOND, 999);
//        return new Timestamp(calendar.getTimeInMillis());
//    }
//    //获取今年是哪一年
//    public static Integer getNowYear() {
//        Date date = new Date();
//        GregorianCalendar gc = (GregorianCalendar) Calendar.getInstance();
//        gc.setTime(date);
//        return Integer.valueOf(gc.get(1));
//    }
//    //获取本月是哪一月
//    public static int getNowMonth() {
//        Date date = new Date();
//        GregorianCalendar gc = (GregorianCalendar) Calendar.getInstance();
//        gc.setTime(date);
//        return gc.get(2) + 1;
//    }
//    //两个日期相减得到的天数
//    public static int getDiffDays(Date beginDate, Date endDate) {
//
//        if (beginDate == null || endDate == null) {
//            throw new IllegalArgumentException("getDiffDays param is null!");
//        }
//
//        long diff = (endDate.getTime() - beginDate.getTime())
//                / (1000 * 60 * 60 * 24);
//
//        int days = new Long(diff).intValue();
//
//        return days;
//    }
//    //两个日期相减得到的毫秒数
//    public static long dateDiff(Date beginDate, Date endDate) {
//        long date1ms = beginDate.getTime();
//        long date2ms = endDate.getTime();
//        return date2ms - date1ms;
//    }
//    //获取两个日期中的最大日期
//    public static Date max(Date beginDate, Date endDate) {
//        if (beginDate == null) {
//            return endDate;
//        }
//        if (endDate == null) {
//            return beginDate;
//        }
//        if (beginDate.after(endDate)) {
//            return beginDate;
//        }
//        return endDate;
//    }
//    //获取两个日期中的最小日期
//    public static Date min(Date beginDate, Date endDate) {
//        if (beginDate == null) {
//            return endDate;
//        }
//        if (endDate == null) {
//            return beginDate;
//        }
//        if (beginDate.after(endDate)) {
//            return endDate;
//        }
//        return beginDate;
//    }
//    //返回某月该季度的第一个月
//    public static Date getFirstSeasonDate(Date date) {
//        final int[] SEASON = { 1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4 };
//        Calendar cal = Calendar.getInstance();
//        cal.setTime(date);
//        int sean = SEASON[cal.get(Calendar.MONTH)];
//        cal.set(Calendar.MONTH, sean * 3 - 3);
//        return cal.getTime();
//    }
//    //返回某个日期下几天的日期
//    public static Date getNextDay(Date date, int i) {
//        Calendar cal = Calendar.getInstance();
//        cal.setTime(date);
//        cal.set(Calendar.DATE, cal.get(Calendar.DATE) + i);
//        return cal.getTime();
//    }
//    //返回某个日期前几天的日期
//    public static Date getFrontDay(Date date, int i) {
//        Calendar cal = Calendar.getInstance();
//        cal.setTime(date);
//        cal.set(Calendar.DATE, cal.get(Calendar.DATE) - i);
//        return cal.getTime();
//    }
//    //获取某年某月到某年某月按天的切片日期集合（间隔天数的日期集合）
//    public static List getTimeList(int beginYear, int beginMonth, int endYear,
//                                   int endMonth, int k) {
//        List list = new ArrayList();
//        if (beginYear == endYear) {
//            for (int j = beginMonth; j <= endMonth; j++) {
//                list.add(getTimeList(beginYear, j, k));
//
//            }
//        } else {
//            {
//                for (int j = beginMonth; j < 12; j++) {
//                    list.add(getTimeList(beginYear, j, k));
//                }
//
//                for (int i = beginYear + 1; i < endYear; i++) {
//                    for (int j = 0; j < 12; j++) {
//                        list.add(getTimeList(i, j, k));
//                    }
//                }
//                for (int j = 0; j <= endMonth; j++) {
//                    list.add(getTimeList(endYear, j, k));
//                }
//            }
//        }
//        return list;
//    }
//    //获取某年某月按天切片日期集合（某个月间隔多少天的日期集合）
//    public static List getTimeList(int beginYear, int beginMonth, int k) {
//        List list = new ArrayList();
//        Calendar begincal = new GregorianCalendar(beginYear, beginMonth, 1);
//        int max = begincal.getActualMaximum(Calendar.DATE);
//        for (int i = 1; i < max; i = i + k) {
//            list.add(begincal.getTime());
//            begincal.add(Calendar.DATE, k);
//        }
//        begincal = new GregorianCalendar(beginYear, beginMonth, max);
//        list.add(begincal.getTime());
//        return list;
//    }
public static void main(String[] args) {

        //测试线程安全
//        for (int i =0;i<100;i++){
//            Runnable runnable = ()->{
//                int a  = (int)(1+Math.random()*(10-1+1));
//                Long[] b ={1553184000000L,1553097600000L,1553011200000L,1552924800000L,1552838400000L,1552752000000L,1552665600000L,1552579200000L,1552492800000L,1552406400000L,1552320000000L};
//                System.out.println("取到的随机数:"+new SimpleDateFormat(DEFAULT_FORMAT).format(new Date(b[a]))+"未调整格式话后:"+defaultFormat.format(new Date(b[a]))+"调整格式化后："+DateUtils.timeStrToString(b[a]));;
//            };
//            new Thread(runnable).start();
//        }
}
}
